package com.atguigu.ssyx.sys;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 杨林
 * @create 2023-07-14 11:37 星期五
 * description:IO测试
 */
@Slf4j
@SpringBootTest
public class IOTest2 {

    /**
     * 分隔符：
     *       File.separator: \            //字符串
     *       File.separatorChar: \        //字符
     *       File.pathSeparator: ;        //字符串
     *       File.pathSeparatorChar: ;    //字符
     */
    @Test
    public void testSeparator(){
        log.info("File.separator:{}", File.separator);
        log.info("File.separatorChar:{}",File.separatorChar);
        log.info("File.pathSeparator:{}",File.pathSeparator);
        log.info("File.pathSeparatorChar:{}",File.pathSeparatorChar);
    }


    /**
     * 字节写文件_1
     */
    @Test
    public void testByteWrite() throws Exception{
        log.info("开始字节写文件");
        /**
         * new FileOutputStream(MyUtils.getResourcePath("io/hello.txt"), false);
         *      参数1：文件名或路径
         *      参数2：指是否已在文件已经存在的情况下追加数据
         *          false：如果文件已经存在，清空文件，并从头写入
         *          true：文件已经存在的情况下，将要写入的数据追加在文件尾部
         *    注意：需要文件提前存在
         */
        //String resourcePath = MyUtils.getResourcePath("io/hello.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\IdeaProject\\ioTest\\hello.txt", false);
        //输出缓冲流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write("hello world!".getBytes("UTF-8"));
        //关闭流：从下往上关闭
        bufferedOutputStream.close();
        fileOutputStream.close();
        log.info("文件写出完毕！");
    }

    /**
     * 测试字符写文件
     */
    @Test
    public void testCharWrite() throws IOException { 
        log.info("字符写文件开始");
        //字符输出流
        FileWriter fileWriter = new FileWriter("D:\\IdeaProject\\ioTest\\hello2.txt", false);
        //字节输出流缓冲流
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("这是一个字符输出流");  //write:将数据输出到缓冲区，并未写入到目标文件
        bufferedWriter.newLine();   //这是换行操作
        bufferedWriter.write("这是新的一行数据");
        //清空缓存：清空缓存时立即写出，并非等缓冲区满了在输出
        //bufferedWriter.flush();
        //关闭流：关闭之前会进行一次flush()操作
        bufferedWriter.close();
        fileWriter.close();
        log.info("字符写文件结束");
    }

    /**
     * 测试字节读文件
     */
    @Test
    public void testByteRead() throws Exception {
        log.info("字节读文件开始");
        //字节输入流
        FileInputStream fileInputStream = new FileInputStream("D:\\IdeaProject\\ioTest\\hello.txt");
        
//        int length;
//        byte[] bytes = new byte[8192];
//        while ((length = fileInputStream.read(bytes)) != -1){
//            log.info(new String(bytes,0,length));
//        }
//        //关闭流
//        fileInputStream.close();
//        log.info("字节读文件结束");
        //缓冲流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        int length;
        byte[] bytes = new byte[8192];
        while ((length = bufferedInputStream.read(bytes)) != -1){
            log.info(new String(bytes,0,length));
        }
        
        //关闭流
        bufferedInputStream.close();
        fileInputStream.close();
        log.info("字节读文件结束");
    }

    /**
     * 测试字符读文件
     */
    @Test
    public void testCharReader() throws Exception { 
        log.info("字符读文件开始");
        FileReader fileReader = new FileReader("D:\\IdeaProject\\ioTest\\hello2.txt");
        fileReader.getEncoding();  //获取文件原来的编码方式
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        
//        int length;
//        char[] chars = new char[8192];
//        while ((length = bufferedReader.read(chars)) != -1){
//            log.info(new String(chars,0,length));   
//        }
//        //关闭流
//        bufferedReader.close();
//        fileReader.close();
//        log.info("字符读文件结束");
        
        //按行读取操作
        String line;   //一次读取一行，不包括换行符
        while ((line = bufferedReader.readLine()) != null){
            log.info(line);
        }
        //关闭流
        bufferedReader.close();
        fileReader.close();
        log.info("字符读文件结束");
    }

    /**
     * 边读边写
     */
    @Test
    public void testReadAndWriter() throws Exception { 
        log.info("开始边读边写");
        //字节读取流
        File fileRead = new File("D:\\IdeaProject\\ioTest\\hello.txt");
        FileInputStream fileInputStream = new FileInputStream(fileRead);
        
        //字节写入流
        File fileWrite = new File("D:\\IdeaProject\\ioTest\\hello3.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(fileWrite);
        
        int length;
        byte[] bytes = new byte[8192];
        while ((length = fileInputStream.read(bytes)) != -1){
            fileOutputStream.write(bytes,0,length);
        }
        fileOutputStream.close();
        fileInputStream.close();
        log.info("边度边写结束");
    }
    
    /**
     * 根据生日获取年龄  与IO无关
     */
    @Test
    public void getAge() throws ParseException {
        String birth = "1999-07-14";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(birth);
        Calendar today = Calendar.getInstance();
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(parse);
        int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        if (today.get(Calendar.MONTH) < birthday.get(Calendar.MONTH) ||
                (today.get(Calendar.MONTH) == today.get(Calendar.MONTH) &&
                        today.get(Calendar.DAY_OF_MONTH) < birthday.get(Calendar.DAY_OF_MONTH))){
            age--;
        }
        System.out.println(age);
    }
}
